package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dk.model.*;
import com.dk.mapper.ScheduldMapper;
import com.dk.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dk.vo.CraftConstituteVO;
import com.dk.vo.EchartsVO;
import com.dk.vo.ProcedureVO;
import com.dk.vo.ScheduldVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wbh
 * @since 2023-03-21
 */
@Service
public class ScheduldServiceImpl extends ServiceImpl<ScheduldMapper, Scheduld> implements ScheduldService {

    @Autowired
    private WorkService workService;
    @Resource
    private ScheduldMapper scheduldMapper;
    @Autowired
    private CraftConstituteService craftConstituteService;
    @Autowired
    private ProcedureService procedureService;
    @Autowired
    private CraftRelatedProductService craftRelatedProductService;
    @Autowired
    private WorkstationsService workstationsService;

    @Override
    public void insert(ScheduldVO scheduldVO) {
        //增加任务表信息,工单id必须传,工作站的id必须传,工艺流程顺序的id必传
        //1.根据工单信息查询,拼接任务名称名字,
        Work work = workService.updateByWorkId(scheduldVO.getWorkId());
        StringBuilder builder = new StringBuilder();
        builder.append(StringUtils.isNotBlank(work.getProductName()) ? work.getProductName() : "");
        builder.append(StringUtils.isNotBlank(work.getWorkSpec()) ? work.getWorkSpec() : "");
        builder.append(StringUtils.isNotBlank(work.getWorkNum().toString()) ? work.getWorkNum() : "");
        scheduldVO.setScheduldName(builder.toString());//拼接名字
        Scheduld scheduld = new Scheduld();
        BeanUtils.copyProperties(scheduldVO, scheduld);
        //通过传过来的constituteId查询pd_craft_constitute
        CraftConstituteVO infoCraftConstituteById = craftConstituteService.getInfoCraftConstituteById(scheduld.getConstituteId());
        //在查询pd_procedure
        ProcedureVO infoProcedureById = procedureService.getInfoProcedureById(infoCraftConstituteById.getProcedureId());
        //set名字
        scheduld.setProduceName(infoProcedureById.getProcedureName());
        scheduldMapper.insert(scheduld);
    }

    @Override
    public List<ScheduldVO> getInfoScheduldByWordId(ScheduldVO scheduldVO) {
        //根据workId查询以及流程排序id
        LambdaQueryWrapper<Scheduld> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(scheduldVO.getWorkId()), Scheduld::getWorkId, scheduldVO.getWorkId());
        wrapper.eq(Scheduld::getConstituteId, scheduldVO.getConstituteId());
        List<ScheduldVO> collect = scheduldMapper.selectList(wrapper).stream().map(scheduld -> {
            ScheduldVO scheduldVO1 = new ScheduldVO();
            BeanUtils.copyProperties(scheduld, scheduldVO1);
            return scheduldVO1;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public ScheduldVO getInfoScheduldById(String scheduldId) {
        Scheduld scheduld = scheduldMapper.selectById(scheduldId);
        ScheduldVO scheduldVO = new ScheduldVO();
        BeanUtils.copyProperties(scheduld, scheduldVO);
        return scheduldVO;
    }

    @Override
    public void updateInfoScheduldById(ScheduldVO scheduldVO) {
        Scheduld scheduld = new Scheduld();
        BeanUtils.copyProperties(scheduldVO, scheduld);
        scheduldMapper.updateById(scheduld);
    }

    @Override
    public void deleteInfoScheduldById(String scheduldId) {
        scheduldMapper.deleteById(scheduldId);
    }


    @Override
    public List<CraftConstituteVO> getInfoCraftConstituteByMaterialId(String materialId) {
        //通过materialId 查询流程id
        CraftRelatedProduct infoCraftRelatedProductByMaterialId = craftRelatedProductService.getInfoCraftRelatedProductByMaterialId(materialId);
        //通过流程id获取工序组成
        String craftRouteId = infoCraftRelatedProductByMaterialId.getCraftRouteId();
        List<CraftConstitute> infoCraftConstituteByRouteId = craftConstituteService.getInfoCraftConstituteByRouteId(craftRouteId);
        //获取到工序id
        List<String> list = new ArrayList<>();
        List<CraftConstituteVO> craftConstituteVOList = new ArrayList<>();
        for (CraftConstitute craftConstitute : infoCraftConstituteByRouteId) {
            CraftConstituteVO vo = new CraftConstituteVO();
            BeanUtils.copyProperties(craftConstitute, vo);
            craftConstituteVOList.add(vo);
            list.add(craftConstitute.getProcedureId());
        }
        //查询工序名称
        List<Procedure> infoListByProcedureIds = procedureService.getInfoListByProcedureIds(list);
        for (CraftConstituteVO craftConstituteVO : craftConstituteVOList) {
            for (Procedure infoListByProcedureId : infoListByProcedureIds) {
                if (StringUtils.equals(craftConstituteVO.getProcedureId(), infoListByProcedureId.getProcedureId())) {
                    craftConstituteVO.setProcedureName(infoListByProcedureId.getProcedureName());
                }
            }
        }
        return craftConstituteVOList;
    }

    @Override
    public List<ScheduldVO> getInfoList() {
        List<Scheduld> schedulds = scheduldMapper.selectList(null);
        Map<String, List<Scheduld>> collect = schedulds.stream().collect(Collectors.groupingBy(Scheduld::getWorkId));
        List<ScheduldVO> list = new ArrayList<>();
        //循环map
        collect.forEach((key, value) -> {
            ScheduldVO vo = new ScheduldVO();
            List<ScheduldVO> collect1 = value.stream().map(scheduld -> {
                ScheduldVO vo1 = new ScheduldVO();
                BeanUtils.copyProperties(scheduld, vo1);
                return vo1;
            }).collect(Collectors.toList());
            vo.setWorkId(key);
            vo.setList(collect1);
            list.add(vo);
        });
        return list;
    }

    @Override
    public String getWorkstationNameByScheduldById(String scheduldId) {
        Scheduld scheduld = scheduldMapper.selectById(scheduldId);
        String workstationsId = scheduld.getWorkstationsId();
        Workstations workstations = workstationsService.selectInfoWorkstationsById(workstationsId);
        return workstations.getWorkstationName();
    }


}
